﻿Public Class CLabelCheckState2 : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelCheckState2.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABELCHECK", "CHNG") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase

        Try


            Dim nextState As CStateBase = Nothing
            Dim pdaTask As CPDATask2 = DirectCast(task, CPDATask2)
            Dim webMng As CWebManager = CWebManager.GetInstance
            Dim checkInfo As CCheck = CCheck.GetIns
            Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

            'Dim waitTask As CPersonal = New CPersonal(pdaTask)
            Debug.WriteLine("CLabelCheckState2.DoExecute...START")

            If task.ProcessText = "" Then
                Dim label1 As String = ""
                Dim statusMsg As String
                If pdaTask.ChkMazdaLabel = "" Then
                    label1 += CConst.CNS_MSG_017 & ","
                End If

                If pdaTask.ChkDNLabel = "" Then
                    label1 += CConst.CNS_MSG_018 & ","
                End If

                If pdaTask.ChkBeanLabel = "" Then
                    label1 += CConst.CNS_MSG_019 & ","
                End If

                If pdaTask.ChkCustLabel = "" Then
                    label1 += CConst.CNS_MSG_020
                End If

                If pdaTask.ChkMazdaLabel = "" And pdaTask.ChkBeanLabel = "" _
                    And pdaTask.ChkCustLabel = "" And pdaTask.ChkDNLabel = "" Then

                    statusMsg = CConst.CNS_MSG_012
                Else
                    statusMsg = CCommon.msgFormat(CConst.CNS_MSG_016, label1)
                End If

                ' Dim statusMsg As String = CCommon.msgFormat(CConst.CNS_MSG_016, label1)
                '' リーダーからデータを取得する(待ち時間制限なし)
                pdaTask.WindowManager.SetMessage(statusMsg, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                task.ProcessText = task.ReadTextWait(-1)
                Debug.WriteLine("読んだバーコード:" & task.ProcessText)

            End If

   
            'ユニークコードチェック()
            If Not checkInfo.ChkUniqueCd_4PK_Data(CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText))) Then

                '重複するuniqueCdがある場合
                Dim uniqueData As CCheck.UniqueCdData = checkInfo.ChkGetUniqueCd_4PK_Data(CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText)))
                Dim readYear As String = uniqueData.actionDateTime.Substring(2, 2)
                Dim readMonth As String = uniqueData.actionDateTime.Substring(5, 2)
                Dim readDay As String = uniqueData.actionDateTime.Substring(8, 2)
                Dim readHour As String = uniqueData.actionDateTime.Substring(11, 2)
                Dim readMin As String = uniqueData.actionDateTime.Substring(14, 2)

                Dim empInfo As CCheck.EmpData = checkInfo.ChkEmpData(uniqueData.empCd)
                '統括コード
                Dim errMsg As String = CCommon.msgFormat(CConst.CNS_MSG_015, readYear, readMonth, readDay, readHour, readMin, empInfo.empName)
                ' 重複するuniqueCdがある場合
                CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, errMsg, Color.White)
                task.ProcessText = ""
                task.ActionCd = "LABELCHECK"

                '保存したデータを消す
                pdaTask.ChkMazdaLabel = ""
                pdaTask.ChkBeanLabel = ""
                pdaTask.ChkCustLabel = ""
                pdaTask.ChkDNLabel = ""
                pdaTask.WindowManager.ClearActionLabel()

                nextState = GetNextState(task)
                Return nextState
            End If

            Debug.WriteLine("CShiporderState2...START")

            Select Case CBarcode.GetBarcodeType(task.ProcessText)
                '取消処理
                Case EnumBarcodeType.CANCEL_CODE
                    If pdaTask.ChkBeanLabel = "" And pdaTask.ChkDNLabel = "" _
                         And pdaTask.ChkCustLabel = "" And pdaTask.ChkMazdaLabel = "" Then

                        CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                            Color.Black, CConst.EnumSound.YESNO_BUZZ, CConst.CNS_MSG_025, Color.White)

                        task.ProcessText = ""
                        task.ActionCd = "LABELCHECK"

                    Else

                        Dim sendFlg As Boolean = CPersonal.ConfirmWindow2(pdaTask, EnumWindowMode.YESNO_MODE, _
                                                                    Color.Black, CConst.EnumSound.YESNO_BUZZ, CConst.CNS_MSG_024, Color.White)
                        If sendFlg Then
                            '保存したデータを消す
                            CPersonal.PdaTaskClear(pdaTask)
                            task.ProcessText = ""
                            task.ActionCd = "LABELCHECK"
                        Else
                            Dim getVl As String = chkGetValue(pdaTask)
                            Dim orderCd As String = pdaTask.WindowManager.GetOrderCd(getVl)
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(orderCd)
                            setData.RowColor = System.Drawing.Color.GreenYellow
                        End If
                    End If

                    '途中送信
                Case EnumBarcodeType.SEND_CODE

                    Dim sendFlg As Boolean = CPersonal.ConfirmWindow2(pdaTask, EnumWindowMode.YESNO_MODE, _
                                                                    Color.Black, CConst.EnumSound.YESNO_BUZZ, CConst.CNS_MSG_008, Color.White)
                    If sendFlg Then
                        '送信データが存在チェック
                        If checkInfo.DataCountCheck("4") Then
                            Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                            CBgControl.BgThread("UNI", "csv", filePath, checkInfo.GetUnique_4PK_Data(pdaTask.PdaNo, "4"), CConst.ORDER_DATA_UPLOAD)
                            pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_009, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                        End If
                    End If

                    task.ProcessText = ""
                    task.ActionCd = "LABELCHECK"

                    '交代
                Case EnumBarcodeType.EMP_CODE

                    Dim EmpData As CCheck.EmpData = checkInfo.ChkEmpData(task.ProcessText)

                    pdaTask.WindowManager.SetEmpInfo(EmpData.empCd & ":[" & EmpData.empName & "]") '
                    pdaTask.EmpCd = EmpData.empCd
                    task.ActionCd = "LABELCHECK"

                    '終了
                Case EnumBarcodeType.END_LABEL_CODE
                    '送信データが存在チェック
                    If checkInfo.DataCountCheck("4") Then
                        Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                        CBgControl.BgThread("UNI", "csv", filePath, checkInfo.GetUnique_4PK_Data(pdaTask.PdaNo, "4"), CConst.ORDER_DATA_UPLOAD)
                        pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_009, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                    End If

                    '終了コード
                    task.ProcessText = ""
                    checkInfo.Release()
                    pdaTask.WindowManager.CloseWindow() ''強制終了
                    task.ActionCd = "LOUT"


                    '出荷指示書
                Case EnumBarcodeType.REPORT_CODE, EnumBarcodeType.GROUP_REPORT_CODE

                    Debug.WriteLine("総統括コードを読みました。")
                    pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_011, CConst.COLOR_BLACK, CConst.COLOR_GREEN)

                    CPersonal.PdaTaskClear(pdaTask)

                    Select Case webMng.getShiporderData(pdaTask, task.ProcessText)
                        '成功
                        Case 0
                            task.ActionCd = "LABELCHECK"
                            pdaTask.Hardware.nextA()
                            '警告
                        Case 1
                            task.ActionCd = "LABELCHECK"
                            pdaTask.Hardware.nextA()
                            'エラー, '警告
                        Case 99, 2
                            task.ProcessText = ""
                            task.ActionCd = "LABELCHECK"
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                    Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_022, Color.White)
                            ''保存したデータを消す
                            CPersonal.PdaTaskClear(pdaTask)

                            Return DoBaseExecute(task)
                    End Select

                    'ＤＮ現品票
                Case EnumBarcodeType.DN_DELIVERY_CODE
                    Dim getVl As String = CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText))

                    'まず、出荷指示データに納品番号があるかどうかを判断
                    If checkInfo.ChkShipData(getVl) And chkValue(pdaTask, getVl) Then
                        If pdaTask.ChkDNLabel = "" Then
                            pdaTask.ChkDNLabel = getVl

                            CPersonal.addWindowLabelBox(CBarcode.GetBarcodeType(task.ProcessText), pdaTask.WindowManager)

                            Dim orderCd As String = pdaTask.WindowManager.GetOrderCd(getVl)
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(orderCd)
                            setData.RowColor = System.Drawing.Color.GreenYellow

                        Else
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                               Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                            task.ActionCd = "LABELCHECK"
                            '保存したデータを消す
                            CPersonal.PdaTaskClear(pdaTask)


                        End If
                    Else
                        CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_021, Color.White)
                        task.ActionCd = "LABELCHECK"
                        '保存したデータを消す
                        CPersonal.PdaTaskClear(pdaTask)
                    End If

                Case EnumBarcodeType.MAZDA_DELIVERY_CODE
                    Dim getVl As String = CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText))

                    'まず、出荷指示データに納品番号があるかどうかを判断
                    If checkInfo.ChkShipData(getVl) And chkValue(pdaTask, getVl) Then
                        If pdaTask.ChkMazdaLabel = "" Then
                            pdaTask.ChkMazdaLabel = getVl

                            CPersonal.addWindowLabelBox(CBarcode.GetBarcodeType(task.ProcessText), pdaTask.WindowManager)

                            Dim orderCd As String = pdaTask.WindowManager.GetOrderCd(getVl)
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(orderCd)
                            setData.RowColor = System.Drawing.Color.GreenYellow

                        Else
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                               Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                            task.ActionCd = "LABELCHECK"
                            '保存したデータを消す
                            CPersonal.PdaTaskClear(pdaTask)
                        End If
                    Else
                        CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_021, Color.White)
                        task.ActionCd = "LABELCHECK"
                        '保存したデータを消す
                        CPersonal.PdaTaskClear(pdaTask)
                    End If

                Case EnumBarcodeType.BEAN_LABEL
                    Dim getVl As String = CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText))
                    'まず、出荷指示データに納品番号があるかどうかを判断
                    If checkInfo.ChkShipData(getVl) And chkValue(pdaTask, getVl) Then
                        If pdaTask.ChkBeanLabel = "" Then
                            pdaTask.ChkBeanLabel = getVl

                            CPersonal.addWindowLabelBox(CBarcode.GetBarcodeType(task.ProcessText), pdaTask.WindowManager)

                            Dim orderCd As String = pdaTask.WindowManager.GetOrderCd(getVl)
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(orderCd)
                            setData.RowColor = System.Drawing.Color.GreenYellow
                        Else
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                               Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                            task.ActionCd = "LABELCHECK"
                            '保存したデータを消す
                            CPersonal.PdaTaskClear(pdaTask)
                        End If
                    Else
                        CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_021, Color.White)
                        task.ActionCd = "LABELCHECK"
                        '保存したデータを消す
                        CPersonal.PdaTaskClear(pdaTask)

                    End If

                Case EnumBarcodeType.CUST_CHECK_CODE
                    Dim getVl As String = CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText))

                    'まず、出荷指示データに納品番号があるかどうかを判断
                    If checkInfo.ChkShipData(getVl) And chkValue(pdaTask, getVl) Then
                        If pdaTask.ChkCustLabel = "" Then
                            pdaTask.ChkCustLabel = getVl

                            CPersonal.addWindowLabelBox(CBarcode.GetBarcodeType(task.ProcessText), pdaTask.WindowManager)

                            Dim orderCd As String = pdaTask.WindowManager.GetOrderCd(getVl)
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(orderCd)
                            setData.RowColor = System.Drawing.Color.GreenYellow
                        Else
                            CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                               Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                            task.ActionCd = "LABELCHECK"
                            '保存したデータを消す
                            CPersonal.PdaTaskClear(pdaTask)
                        End If
                    Else
                        CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_021, Color.White)
                        task.ActionCd = "LABELCHECK"
                        '保存したデータを消す
                        CPersonal.PdaTaskClear(pdaTask)
                    End If

                Case Else
                    task.ProcessText = ""
                    CPersonal.ErrorWindow2(pdaTask, EnumWindowMode.ALERT_MODE, _
                                            Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_021, Color.White)
                    task.ActionCd = "LABELCHECK"
                    '保存したデータを消す
                    CPersonal.PdaTaskClear(pdaTask)

            End Select

            If pdaTask.ChkBeanLabel = pdaTask.ChkCustLabel And pdaTask.ChkDNLabel = pdaTask.ChkMazdaLabel _
                And Not pdaTask.ChkCustLabel = "" And Not pdaTask.ChkDNLabel = "" Then

                pdaTask.MazadaLabel = pdaTask.ChkMazdaLabel
                task.ActionCd = "LABEL-IN"
            Else
                task.ActionCd = "LABELCHECK"
            End If


            task.ProcessText = ""
            nextState = GetNextState(task)
            Debug.WriteLine("CLabelCheckState2.DoExecute...END")
            Return nextState

        Catch ex As Exception
            'MessageBox.Show("error")
            'MessageBox.Show(ex.Message)
        End Try

    End Function


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="pdaTask"></param>
    ''' <param name="strValue"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function chkValue(ByVal pdaTask As CPDATask2, ByVal strValue As String) As Boolean

        If Not pdaTask.ChkBeanLabel = "" Then
            If pdaTask.ChkBeanLabel = strValue Then
                Return True
            Else
                Return False
            End If
        End If

        If Not pdaTask.ChkCustLabel = "" Then
            If pdaTask.ChkCustLabel = strValue Then
                Return True
            Else
                Return False
            End If
        End If

        If Not pdaTask.ChkDNLabel = "" Then
            If pdaTask.ChkDNLabel = strValue Then
                Return True
            Else
                Return False
            End If
        End If

        If Not pdaTask.ChkMazdaLabel = "" Then
            If pdaTask.ChkMazdaLabel = strValue Then
                Return True
            Else
                Return False
            End If
        End If
        Return True
    End Function

    Private Function chkGetValue(ByVal pdaTask As CPDATask2) As String

        If Not pdaTask.ChkBeanLabel = "" Then
            Return pdaTask.ChkBeanLabel
        End If

        If Not pdaTask.ChkCustLabel = "" Then
            Return pdaTask.ChkCustLabel
        End If

        If Not pdaTask.ChkDNLabel = "" Then
            Return pdaTask.ChkDNLabel
        End If

        If Not pdaTask.ChkMazdaLabel = "" Then
            Return pdaTask.ChkMazdaLabel
        End If
        Return Nothing

    End Function
    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelCheckState.DoExit")
        Return True
    End Function


End Class
